<?php

/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 * 
 */

function sendTeamMail($user, $oldteam) {
	global $loader;
	global $LANG;
	global $MAIL;

	$u = new user;
	$u->teamid = $user->teamid;
	$u->groupid = GROUP_COORD;
	$list = $loader->loadListBy($u, $u->listByFilters());

	if (isset ($oldteam)) {
		$new = true;
	}

	$ar = array (
		$user->userid,
		isset ($oldteam) ? $oldteam->name : ""
	);

	foreach ($list as $key => $obj) {
		include (ROOT_PATH . "lang/mail_lang_" . $obj->lang . ".inc");
		if ($new === true) {
			$text = createMessage($MAIL['switchteamuser'], $ar);
			$subject = createMessage($MAIL['switchteamusersub'], $ar);
		} else {
			$text = createMessage($MAIL['newusermail'], $ar);
			$subject = createMessage($MAIL['newusermailsub'], $ar);
		}
		sendMail($obj->mail, $subject, $text, $user->mail);
	}

	/* NOTIFY THE ADMINS */
	$team = new team;
	$team->id = $user->teamid;
	$loader->get($team);

	$ar = array (
		$user->userid,
		$team->name
	);

	$u->groupid = GROUP_ADMIN;
	$list = $loader->loadListBy($u, $u->listByGroup());
	foreach ($list as $key => $obj) {
		include (ROOT_PATH . "lang/mail_lang_" . $obj->lang . ".inc");
		$text = createMessage($MAIL['newusermailadmin'], $ar);
		$subject = createMessage($MAIL['newusermailsubadmin'], $ar);
		sendMail($obj->mail, $subject, $text, $user->mail);
	}

}

function sendFileMail($item, $file, $comment = "", $currentu, $filelist) {
	global $loader;
	global $LANG;
	global $MAIL;

	$u = new user;
	$u->teamid = $currentu->teamid;
	$u->groupid = array (
		GROUP_COORD,
		GROUP_SVN
	);
	$list = $loader->loadListBy($u, $u->listByFilters());

	$attach = chunk_split(base64_encode(file_get_contents($file)));
	$hash = md5(time());
	/*
	$headers = "From: ".APPLICATION_NAME." <".SENDER_ADDRESS.">\r\n";
	$headers .= "Reply-To: ".$currentu->userid." <".$currentu->mail.">\r\n";
	$headers .= "MIME-Version: 1.0\r\n";
	$headers .= "Content-Type: multipart/mixed; ";
	$headers .= "boundary=".$hash."\r\n";
	$headers .= "--$hash\r\n";
	$headers .= "Message-ID: <".$hash." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n";
	$headers .= "X-Mailer: PHP v".phpversion()."\r\n";      
	$headers .= "Content-Type:"."application/octet-stream"." ";
	$headers .= "name=\"".$item->fname."\"\r\n";
	$headers .= "Content-Transfer-Encoding: base64\r\n";
	$headers .= "Content-Disposition: attachment; ";
	$headers .= "filename=\"".$item->fname."\"\r\n";
	$headers .= "".$attach."\r\n";
	$headers .= "--".$hash."--";            		
	*/
	$link = HOME . "?action=filecheckin";

	$ar = array (
		$currentu->userid
	);
	$ar2 = array (
		$link,
		"\"" . $comment . "\""
	);

	$listtext = "";
	foreach ($filelist as $f) {
		$listtext .= "* " . $f->name . " - [" . $f->path . "]\n";
	}
	$text = "";
	foreach ($list as $key => $obj) {
		include (ROOT_PATH . "lang/mail_lang_" . $obj->lang . ".inc");
		$subject = createMessage($MAIL['commitrequestsub'], $ar);
		$text = $listtext . "\n" . createMessage($MAIL['commitrequest'], $ar2);
		mail_attachment($item->fname, $file, $obj->mail, SENDER_ADDRESS, APPLICATION_NAME, $currentu->mail, $subject, $text);
		//$r = mail($obj->mail, $subject, $text,$headers);

	}

}

function sendMail($to, $subject, $text, $replyto, $replytoname) {
	$headers = "From: " . APPLICATION_NAME . " <" . SENDER_ADDRESS . ">\r\n";
	if (isset ($replytoname)) {
		$headers .= "Reply-To: " . $replytoname . " <" . $replyto . ">\r\n";
	}
	elseif (isset ($replyto)) {
		$headers .= "Reply-To: " . $replyto . "\r\n";
	}
	$headers .= "MIME-Version: 1.0\r\n";
	$headers .= "Content-type: text/plain; charset=\"iso-8859-1\"\r\n";
	$headers .= "Content-Transfer-Encoding: 8bit\r\n";
	$headers .= "Content-Disposition: inline\r\n";
	$headers .= "Return-Path: " . APPLICATION_NAME . " <" . SENDER_ADDRESS . ">\r\n";
	$headers .= "Message-ID: <" . md5(time()) . "-" . SENDER_ADDRESS . ">\r\n";
	$headers .= "X-Mailer: PHP v" . phpversion() . "\r\n";

	maillog($subject . '<br>' . $text);
	$r = mail($to,mb_encode_mimeheader($subject,"iso-8859-1", "B", "\n"), utf8_decode($text), $headers);
}

function notifyUser($uid, $file) {
	global $loader;
	global $LANG;

	$u = new user;
	$u->userid = $uid;
	$loader->get($u);

	$spr = new project;
	$spr->id = $file->subprj;
	$loader->get($spr);

	$prj = new project;
	$prj->id = $spr->parent;
	$loader->get($prj);

	include (ROOT_PATH . "lang/mail_lang_" . $u->lang . ".inc");

	$ar = array (
		$file->name,
		$prj->name,
		$spr->name
	);
	$subs = createMessage($MAIL['assignmailsub'], $ar);
	$text = createMessage($MAIL['assignmail'], $ar) . "\n\n" . $LANG['comment'] . ": " . $file->comment;

	sendMail($u->mail, $subs, $text);

}

function sendPasswordMail($user, $pwd) {
	global $loader;

	include (ROOT_PATH . "lang/mail_lang_" . $user->lang . ".inc");

	$link = HOME . "?rc=" . $pwd . "&u=" . $user->userid;

	$ar = array (
		RECOVERY_EXPIRE
	);
	$text = createMessage($MAIL['pwdrecoverymail'], $ar) . "\n" . $link;
	$subject = $MAIL['pwdrecoverysub'];

	sendMail($user->mail, $subject, $text);

}

function sendConfirmMail($user, $pwd) {
	global $loader;

	include (ROOT_PATH . "lang/mail_lang_" . $user->lang . ".inc");

	$link = HOME . "?rc=" . $pwd . "&u=" . $user->userid;

	$ar = array (
		RECOVERY_EXPIRE
	);
	$text = createMessage($MAIL['confirmmail'], $ar) . "\n" . $link;
	$subject = $MAIL['confirmmailsub'];

	sendMail($user->mail, $subject, $text);

}

function sendRejectMail($user, $userid, $filelist, $fname) {
	global $loader;

	$u = new user;
	$u->userid = $userid;
	$loader->get($u);

	include (ROOT_PATH . "lang/mail_lang_" . $u->lang . ".inc");

	$ar = array (
		$user->userid
	);
	$ar2 = array (
		$fname
	);
	$subject = createMessage($MAIL['rejectfilesub'], $ar2);
	$text = createMessage($MAIL['rejectfile'], $ar) . "\n\n";
	foreach ($filelist as $f) {
		$filetext .= "* " . $f->name . " - " . $f->path . "\n";
	}

	$text .= $filetext;

	sendMail($u->mail, $subject, $text);

	$us = new user;
	$us->teamid = $u->teamid;
	$us->groupid = array (
		GROUP_COORD,
		GROUP_SVN
	);
	$list = $loader->loadListBy($us, $us->listByFilters());

	foreach ($list as $key => $obj) {
		if ($obj->userid != $user->userid) {
			include (ROOT_PATH . "lang/mail_lang_" . $obj->lang . ".inc");
			$subject = createMessage($MAIL['rejectfilesub'], $ar2);
			$text = createMessage($MAIL['rejectfile'], $ar) . "\n\n";
			$text .= $filetext;
			sendMail($obj->mail, $subject, $text);
		}
	}

}

function sendCommitedMail($userid, $user, $filelist, $fname) {
	global $loader;

	$u = new user;
	$u->userid = $userid;
	$loader->get($u);

	include (ROOT_PATH . "lang/mail_lang_" . $u->lang . ".inc");

	$ar2 = array (
		$fname,
		$user->userid
	);
	$subject = createMessage($MAIL['filecommitedsub'], $ar2);
	$comment = "";
	foreach ($filelist as $f) {
		$filetext .= "* " . $f->name . " - " . $f->path . "\n";
		$comment = $f->comment;
	}
	$ar = array (
		$user->userid
	);
	$text = createMessage($MAIL['filecommited'], $ar) . "\n\n";
	$text .= $filetext;
	$text .= $MAIL['filecommited2'] . $comment;

	sendMail($u->mail, $subject, $text);

	$us = new user;
	$us->teamid = $u->teamid;
	$us->groupid = array (
		GROUP_COORD,
		GROUP_SVN
	);
	$list = $loader->loadListBy($us, $us->listByFilters());

	foreach ($list as $key => $obj) {
		if ($obj->userid != $user->userid) {
			include (ROOT_PATH . "lang/mail_lang_" . $obj->lang . ".inc");
			$subject = createMessage($MAIL['filecommitedsub'], $ar2);
			$text = createMessage($MAIL['filecommited'], $ar) . "\n\n";
			$text .= $filetext;
			$text .= $MAIL['filecommited2'] . $comment;
			sendMail($obj->mail, $subject, $text);
		}
	}

}

function sendUserStatusMail($user) {
	global $loader;

	include (ROOT_PATH . "lang/mail_lang_" . $user->lang . ".inc");

	if ($user->status == STATUS_ACTIVE) {
		$status = $MAIL['activated'];
	} else {
		$status = $MAIL['deactivated'];
	}
	$ar = array (
		$status
	);

	$text = createMessage($MAIL['userstatuschange'], $ar);
	$subject = createMessage($MAIL['userstatuschange'], $ar);

	sendMail($user->mail, $subject, $text);

}

function regReceived($user, $msg) {
	global $loader;

	include (ROOT_PATH . "lang/mail_lang_" . $user->lang . ".inc");

	$ar = array (
		$user->userid
	);
	$text = createMessage($MAIL[$msg], $ar);
	$subject = $MAIL['regreceivedsub'];

	sendMail($user->mail, $subject, $text);

}

function sendmp($from, $user, $message) {
	include (ROOT_PATH . "lang/mail_lang_" . $user->lang . ".inc");
	$subject = createMessage($MAIL['mp'], array (
		$from->userid
	));
	sendMail($user->mail, $subject, $message, $from->mail, $form->userid);
}

function createMessage($string, $array) {

	for ($i = 0; $i < count($array); $i++) {
		$string = str_replace("&" . $i, $array[$i], $string);
	}

	return $string;
}

function createMessageKey($string, $array) {
	foreach ($array as $key => $value) {
		$string = str_replace($key, $value, $string);
	}
	return $string;
}

function sendStatUpdateMail($file, $prj) {
	global $loader;
	global $LANG;

	$u = new user;
	$u->teamid = $file->teamid;
	$u->groupid = GROUP_COORD;
	$u->status = STATUS_ACTIVE;
	$list = $loader->loadListBy($u, $u->listByFilters());

	$uas = new user;
	$uas->userid = $file->userid;
	$loader->get($uas);

	array_push($list, $uas);

	$spr = new project;
	$spr->id = $file->subprj;
	$loader->get($spr);
	$ar = array (
		$file->name,
		$prj->name,
		$spr->name
	);

	foreach ($list as $key => $obj) {
		include (ROOT_PATH . "lang/mail_lang_" . $obj->lang . ".inc");
		$text = createMessage($MAIL['statsupdatedfile'], $ar);
		$subject = $MAIL['statsupdatedfilesub'] . " [" . $file->name . "]";
		sendMail($obj->mail, $subject, $text);
	}

}

function sendNotifyStatUpdateMail($user, $list) {

	include (ROOT_PATH . "lang/mail_lang_" . $user->lang . ".inc");
	$subject = $MAIL['statsupdatedfilesub'];
	$text = $MAIL['statsupdatedfile'] . "\n\n";

	foreach ($list as $obj) {
		$ar = array (
			$obj['file']->name,
			$obj['prj']->name,
			$obj['sprj']->name
		);
		$text .= createMessage($MAIL['statsupdatefileline'], $ar) . "\n";
	}

	sendMail($user->mail, $subject, $text);
}

function sendFileChangeNotification($file) {
	global $loader;
	global $LANG;

	$u = new user;
	$u->teamid = $file->teamid;
	$u->status = STATUS_ACTIVE;
	$list = $loader->loadListBy($u, $u->listByFilters());

	$spr = new project;
	$spr->id = $file->subprj;
	$loader->get($spr);

	$prj = new project;
	$prj->id = $spr->parent;
	$loader->get($prj);

	foreach ($list as $key => $obj) {
		if (($obj->userid != $file->userid or $file->statusid == FILE_STATUS_LOCKED or $file->statusid == FILE_STATUS_PROOF) and $obj->notifyOnChange()) {
			include (ROOT_PATH . "lang/mail_lang_" . $obj->lang . ".inc");
			$ar = array (
				$file->name,
				$prj->name,
				$spr->name,
				$MAIL[$file->statusid],
				$file->userid,
				date('Y-m-d H:i:s', time())
			);
			$text = createMessage($MAIL['fileupdated'], $ar);
			sendMail($obj->mail, $text, "");
		}
	}

}

function sendFileListMail($user, $list) {
	global $LANG;

	include (ROOT_PATH . "lang/mail_lang_" . $user->lang . ".inc");

	$subject = $MAIL['assignlistsub'];
	$text = $MAIL['assignlist'];

	foreach ($list as $item) {
		$text .= "=> " . $item->name . " [" . $LANG['untranslated'] . ": " . $item->untranslated . " - " . $LANG['fuzzy'] . ": " . $item->fuzzy . "]\n";
	}

	sendMail($user->mail, $subject, $text);
}

function switchTeamMail($user) {
	global $loader;
	global $LANG;
	global $MAIL;

	$u = new user;
	$u->teamid = $user->teamid;
	$u->groupid = GROUP_COORD;
	$list = $loader->loadListBy($u, $u->listByFilters());

	$team = new team;
	$team->id = $user->teamid;
	$loader->get($team);

	$ar = array (
		$user->userid,
		$team->name
	);

	foreach ($list as $key => $obj) {
		include (ROOT_PATH . "lang/mail_lang_" . $obj->lang . ".inc");
		$text = createMessage($MAIL['teamswitch'], $ar);
		$subject = $text;
		sendMail($obj->mail, $subject, $text, $user->mail);
	}

	/* NOTIFY THE ADMINS */
	$u->groupid = GROUP_ADMIN;
	$list = $loader->loadListBy($u, $u->listByGroup());

	foreach ($list as $key => $obj) {
		include (ROOT_PATH . "lang/mail_lang_" . $obj->lang . ".inc");
		$text = createMessage($MAIL['teamswitch'], $ar);
		$subject = $text;
		sendMail($obj->mail, $subject, $text, $user->mail);
	}

	include (ROOT_PATH . "lang/mail_lang_" . $user->lang . ".inc");

	$text = createMessage($MAIL['teamswitchconfirm'], $ar);
	$subject = $text;

	sendMail($user->mail, $subject, $text);

}

function mail_attachment($filename, $file, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
	$file_size = filesize($file);
	$handle = fopen($file, "r");
	$content = fread($handle, $file_size);
	fclose($handle);
	$content = chunk_split(base64_encode($content));
	$uid = md5(uniqid(time()));
	$header = "From: " . $from_name . " <" . $from_mail . ">\r\n";
	$header .= "Reply-To: " . $replyto . "\r\n";
	$header .= "Return-Path: " . $from_name . " <" . $from_mail . ">\r\n";
	$header .= "Message-ID: <" . $uid . "-" . $from_mail . ">\r\n";
	$header .= "X-Mailer: PHP v" . phpversion() . "\r\n";
	$header .= "MIME-Version: 1.0\r\n";
	$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
	$header .= "This is a multi-part message in MIME format.\r\n";
	$header .= "--" . $uid . "\r\n";
	$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
	$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
	$header .= $message . "\r\n\r\n";
	$header .= "--" . $uid . "\r\n";
	$header .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"\r\n"; // use diff. tyoes here
	$header .= "Content-Transfer-Encoding: base64\r\n";
	$header .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n";
	$header .= $content . "\r\n\r\n";
	$header .= "--" . $uid . "--";
	$r = mail($mailto, $subject, "", $header);
}

function sendGlossaryNotification($new, $old, $user) {
	global $loader;
	global $LANG;

	$u = new user;
	$u->teamid = $user->teamid;
	$u->status = STATUS_ACTIVE;
	$list = $loader->loadListBy($u, $u->listByFilters());

	$maildata = array (
		"&newterm" => $new->eterm,
		"&newtranslation" => $new->translation,
		"&newcontext" => $new->context,
		"&newrelated" => $new->related
	);
	if ($old != null) {
		$olddata = array (
			"&oldterm" => $old->eterm,
			"&oldtranslation" => $old->translation,
			"&oldcontext" => $old->context,
			"&oldrelated" => $old->related
		);
		$maildata = array_merge($maildata, $olddata);
	}

	foreach ($list as $key => $obj) {
		if ($obj->userid != $user->userid and $obj->notifyGlossary()) {
			include (ROOT_PATH . "lang/mail_lang_" . $obj->lang . ".inc");
			$ar = array (
				$user->userid
			);
			$subject = $MAIL['glossaryupdatesub'];
			$text = createMessage($MAIL['glossaryupdate'], $ar);
			if ($old == null) {
				$text .= createMessageKey($MAIL['glossarynew'], $maildata);
			} else {
				$text .= createMessageKey($MAIL['glossaryedit'], $maildata);
			}
			sendMail($obj->mail, $subject, $text);
		}
	}

}

function taskNotification($user, $task, $type) {
	include (ROOT_PATH . "lang/mail_lang_" . $user->lang . ".inc");
	$ar = array (
		date('Y-m-d', time()),
		$MAIL[$type],
		"\"" . $task->task . "\""
	);
	$text = createMessage($MAIL['tasknotification'], $ar);
	$subject = $text;
	sendMail($user->mail, $subject, $text);
}

function sendInvitationMail($temporal, $lang, $replyTo) {
	global $loader;
	
	$team = new team;
	$team->id=$temporal->teamid;
	$loader->get($team);
	
	include (ROOT_PATH . "lang/mail_lang_" . $lang . ".inc");
	$link = HOME . "?action=reguser&auth=".$temporal->id;

	$ar = array($team->name,date('Y-m-d H:i',$temporal->expire)." UTC");
	
	$text = createMessage($MAIL['reginvite'],$ar) . "\n\n" .$link;
	$subject= $MAIL['reginvitesub'];
	sendMail($temporal->extra, $subject, $text,$replyTo);
}

?>
